//
//  UIStartLodingView.m
//  YWToastView
//
//  Created by 余伟 on 16/12/18.
//  Copyright © 2016年 yuwei. All rights reserved.
//

#import "UIStartLodingView.h"
#define StartLodingViewleftViewViewHeight 45

@interface UIStartLodingView ()
{
  NSMutableArray   *_stateArray;//用于存储每个状态的对应的数据
}
@end
@implementation UIStartLodingView
#pragma mark 创建对象
+ (UIStartLodingView *)sharedLodingView
{
    static UIStartLodingView *_sharedTostView = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
       _sharedTostView = [[UIStartLodingView alloc] initWithFrame:CGRectZero];
    });
    return _sharedTostView;
}
-(instancetype)initWithFrame:(CGRect)frame
{
    if (self=[super initWithFrame:frame])
    {
//        self.contentMode = UIViewContentModeCenter;
//        self.autoresizingMask = UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin
//								| UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin;
        [self setBackgroundColor:[UIColor whiteColor]];
        [self addSubview:self.imageView];
        [self addSubview:self.describeLable];
        [self addSubview:self.lodingImageView];
        [self addSubview:self.reloadButton];
        [self.reloadButton setHidden:YES];
        [self bringSubviewToFront:self.describeLable];
    }
    return self;
}
-(void)setMessageString:(NSString *)MessageString
          deslableColor:(UIColor *)textColor
            imageString:(NSString *)imageString
               forState:(YeeStartLodingState)state
{
    [self.lodingImageView.layer removeAllAnimations];
    self.lodingImageView.hidden=YES;
    self.reloadButton.hidden=YES;
    self.describeLable.textColor=textColor?textColor:StartLodingViewTextColor;
    switch (state)
    {
        case YeeStartIsLodingState:
        {
            self.lodingImageView.hidden=NO;
            [self.lodingImageView.layer addAnimation:[self animation] forKey:@"lodingImageViewrate"];
            [self.imageView setImage:[UIImage imageNamed:imageString]];
            self.describeLable.text=MessageString;
        }
            break;
        case YeeStartHaveNoDataState:
        {
            self.lodingImageView.hidden=YES;
            self.reloadButton.hidden=YES;
            [self.imageView setImage:[UIImage imageNamed:imageString]];
            self.describeLable.text=MessageString;

        }
            break;
        case YeeStartHaveNoNetWorkState:
        {
            self.lodingImageView.hidden=YES;
            self.reloadButton.hidden=NO;
            [self.imageView setImage:[UIImage imageNamed:imageString]];
            self.describeLable.text=MessageString;
        }
            break;
            
        default:
            break;
    }
    
    [self setNeedsLayout];
}
#pragma mark 添加子视图
-(UIImageView *)imageView
{
    if (_imageView==nil)
    {
        _imageView=[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, StartLodingViewimageViewWidth, StartLodingViewimageViewHeight)];
        _imageView.contentMode=UIViewContentModeScaleAspectFill;
        _imageView.clipsToBounds=YES;
       
    }
    return _imageView;
}
-(UILabel *)describeLable
{
    if (_describeLable==nil)
    {
        _describeLable=[[UILabel alloc] init];
        _describeLable.font=StartLodingViewDescibeFont;
        _describeLable.numberOfLines=0;//不限行数
        _describeLable.text=@"";
        _describeLable.textAlignment=NSTextAlignmentCenter;
        _describeLable.textColor=StartLodingViewTextColor;
        _describeLable.center=self.center;
        _describeLable.centerY=self.lodingImageView.centerY;
    }
    return _describeLable;
}
-(UIImageView *)lodingImageView
{
    if (_lodingImageView==nil)
    {
        _lodingImageView=[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, StartLodingViewleftViewViewHeight, StartLodingViewleftViewViewHeight)];
        [_lodingImageView setImage:[UIImage imageNamed:@"loading_imgBlue_78x78"]];
        _lodingImageView.backgroundColor = [UIColor clearColor];
        _lodingImageView.contentMode = UIViewContentModeScaleAspectFill;
        _lodingImageView.userInteractionEnabled = NO;
        _lodingImageView.accessibilityIdentifier = @"empty set background image";
    }
    return _lodingImageView;
}
-(UIButton *)reloadButton
{
    if (_reloadButton==nil)
    {
       _reloadButton=[UIButton buttonWithType:UIButtonTypeCustom];
       _reloadButton.layer.masksToBounds=YES;
       _reloadButton.layer.cornerRadius=17.50;
       _reloadButton.layer.borderWidth=0.3f;
       _reloadButton.layer.borderColor=[UIColor grayColor].CGColor;
       _reloadButton.titleLabel.font=StartLodingViewDescibeFont;
      [_reloadButton setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
       [_reloadButton setTitle:@"重新加载" forState:UIControlStateNormal];
        
    }
    return _reloadButton;
}
-(void)didMoveToSuperview
{
    [super didMoveToSuperview];
}
-(CABasicAnimation*)animation
{
    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform"];
    animation.fromValue = [NSValue valueWithCATransform3D:CATransform3DIdentity];
    animation.toValue = [NSValue valueWithCATransform3D: CATransform3DMakeRotation(M_PI_2, 0.0, 0.0, 1.0) ];
    animation.duration = 0.25;
    animation.cumulative = YES;
    animation.repeatCount = MAXFLOAT;
    return animation;
}
-(void)layoutSubviews
{
    [super layoutSubviews];
    
    self.imageView.center=self.center;
    self.imageView.centerY=self.centerY-StartLodingViewimageViewHeight*0.5;
    self.describeLable.frame=CGRectMake(0, 0, self.width-StartLodingViewleftViewViewHeight-StartLodingViewleftmargin*2.0, [self sizeWithString:self.describeLable.text font:StartLodingViewDescibeFont constrainedToWidth:self.width-StartLodingViewleftViewViewHeight-StartLodingViewleftmargin*2.0]);
    self.describeLable.center=self.center;
    self.describeLable.centerY=self.imageView.centerY+StartLodingViewimageViewHeight;
    
    self.lodingImageView.centerY=self.imageView.centerY+self.imageView.height;
    self.lodingImageView.centerX=self.lodingImageView.width+StartLodingViewleftmargin;
    
    self.reloadButton.frame=CGRectMake(0, self.describeLable.bottom+35,90, 35);
    self.reloadButton.centerX=self.centerX;
   
}
-(void)dealloc
{
    [self.lodingImageView.layer removeAllAnimations];
}
#pragma mark - 计算文本
-(CGFloat)sizeWithString:(NSString *)string font:(UIFont *)font constrainedToWidth:(CGFloat)width
{
    UIFont *textFont = font ? font : [UIFont systemFontOfSize:[UIFont systemFontSize]];
    
    CGSize textSize;
    
#if __IPHONE_OS_VERSION_MIN_REQUIRED < 70000
    if ([string respondsToSelector:@selector(boundingRectWithSize:options:attributes:context:)])
    {
        NSMutableParagraphStyle *paragraph = [[NSMutableParagraphStyle alloc] init];
        paragraph.lineBreakMode = NSLineBreakByWordWrapping;
        NSDictionary *attributes = @{NSFontAttributeName: textFont,
                                     NSParagraphStyleAttributeName: paragraph};
        textSize = [string boundingRectWithSize:CGSizeMake(width, CGFLOAT_MAX)
                                        options:(NSStringDrawingUsesLineFragmentOrigin |
                                                 NSStringDrawingTruncatesLastVisibleLine)
                                     attributes:attributes
                                        context:nil].size;
    } else
    {
        textSize = [string sizeWithFont:textFont
                      constrainedToSize:CGSizeMake(width, CGFLOAT_MAX)
                          lineBreakMode:NSLineBreakByWordWrapping];
    }
#else
    NSMutableParagraphStyle *paragraph = [[NSMutableParagraphStyle alloc] init];
    paragraph.lineBreakMode = NSLineBreakByWordWrapping;
    NSDictionary *attributes = @{NSFontAttributeName: textFont,
                                 NSParagraphStyleAttributeName: paragraph};
    textSize = [string boundingRectWithSize:CGSizeMake(width, CGFLOAT_MAX)
                                    options:(NSStringDrawingUsesLineFragmentOrigin |
                                             NSStringDrawingTruncatesLastVisibleLine)
                                 attributes:attributes
                                    context:nil].size;
#endif
    
    return CGSizeMake(ceil(textSize.width), ceil(textSize.height)).height;
}

@end
